package org.shijiagen.cart.contorller;

import lombok.RequiredArgsConstructor;
import org.shijiagen.cart.service.CartService;
import org.shijiagen.param.CartListParam;
import org.shijiagen.param.CartSaveParam;
import org.shijiagen.pojo.Cart;
import org.shijiagen.utils.R;
import org.shijiagen.vo.CartVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("cart")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    @PostMapping("list")
    public R list(@RequestBody @Validated CartListParam cartListParam){
        return R.ok("购物车查询完成",cartService.list(cartListParam.getUserId()));
    }

    @PostMapping("save")
    public R save(@RequestBody @Validated CartSaveParam cartSaveParam){
        R exist = new R();
        exist.setCode("002");
        exist.setMsg("该商品已在购物车中，数量+1");
        CartVo cartVo = cartService.save(cartSaveParam);
        return cartVo == null ? exist : R.ok("购物车新增商品成功",cartVo);
    }

    @PostMapping("update")
    public R update(@RequestBody Cart cart){
        return cartService.update(cart) ? R.ok("购物车商品修改成功") : R.fail("购物车商品修改失败");
    }

    @PostMapping("remove")
    public R remove(@RequestBody Cart cart){
        return cartService.remove(cart) ? R.ok("购物车商品删除成功") : R.fail("购物车商品删除失败");
    }

    @PostMapping("remove/check")
    public R removeCheck(@RequestBody Integer productId){
        return cartService.check(productId);
    }
}
